package com.yihuier.yihuier.Mapper.MeetingManager;

import com.yihuier.yihuier.Entity.MeetingManager.MeetingApply;
import org.apache.ibatis.annotations.*;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

@Mapper
public interface MeetingApplyMapper {

    @Update("UPDATE bz_meeting_apply SET status = #{status} WHERE app_meeting_id = #{app_meeting_id};")
    int updateMeetingApplyStatus(@Param("app_meeting_id")int app_meeting_id,@Param("status")int status);

    @Insert("INSERT INTO bz_meeting_apply(app_name,meeting_time,during_time,em_number,em_list,status,applicant,hardware_applicant,isIntelligence_allocation,other,app_description) " +
            "VALUES(#{meetingName},#{meeting_time},#{during_time},#{em_number},#{em_list},#{status},#{applicant},#{hardware},#{isIntellgence},#{other},#{description}); ")
    int addOneBasicMeetingApply(
            @Param("meetingName") String meetingName
            ,@Param("meeting_time") Timestamp meeting_time
            ,@Param("during_time") int during_time
            ,@Param("em_number") int em_number
            ,@Param("em_list") String em_list
            ,@Param("status")int status
            ,@Param("applicant")int user_id
            ,@Param("hardware") String hardware
            ,@Param("isIntellgence") int isIntellgence
            ,@Param("other") String other
            ,@Param("description")String description);

    @Update("UPDATE bz_meeting_apply SET location = #{location} WHERE app_meeting_id = #{app_meeting_id};")
    int updateMeetingApplyLocation(@Param("app_meeting_id")int app_meeting_id,@Param("location")int location);

    @Select("SELECT MAX(app_meeting_id) FROM bz_meeting_apply;")
    int selectOneLastMeetingApplyId();

    @Select("SELECT app_meeting_id,app_name,app_description,meeting_time,during_time,em_number,em_list,hardware_applicant,isIntelligence_allocation,other,location FROM `bz_meeting_apply` WHERE applicant = #{user_id};")
    List<MeetingApply> selectPersonalApplyMeetings(@Param("user_id") int user_id);

    /**
     * 获取该用户申请会议的数量
     * @param user_id 用户ID
     * @return 申请会议的数量
     */
    @Select("SELECT COUNT(app_meeting_id) FROM bz_meeting_apply WHERE applicant = #{applicant};")
    int selectNumOfMeetingApplyByApplicant(@Param("applicant") int user_id);

    @Results({
            @Result(property = "app_meeting_id",column = "app_meeting_id"),
            @Result(property = "app_name",column = "app_name"),
            @Result(property = "app_description",column = "app_description"),
            @Result(property = "meeting_time",column = "meeting_time"),
            @Result(property = "during_time",column = "during_time"),
            @Result(property = "location",column = "location"),
            @Result(property = "em_number",column = "em_number"),
            @Result(property = "em_list",column = "em_list"),
            @Result(property = "status",column = "status"),
            @Result(property = "applicant",column = "applicant"),
            @Result(property = "hardware_applicant",column = "hardware_applicant"),
            @Result(property = "isIntelligence_allocation",column = "isIntelligence_allocation"),
            @Result(property = "other",column = "other"),
    })
    @Select("SELECT app_meeting_id,app_name,app_description,meeting_time,during_time,location,em_number,em_list,status,applicant,hardware_applicant,isIntelligence_allocation,other " +
            "FROM bz_meeting_apply WHERE status = #{status} AND applicant IN (SELECT user_id FROM bz_department_user WHERE department_id IN " +
            "(SELECT department_id FROM bz_department WHERE department_belongs_company = (SELECT department_belongs_company FROM bz_department WHERE department_id = " +
            "(SELECT department_id FROM bz_department_user WHERE user_id = #{applicant}))));")
    List<MeetingApply> selectMeetingApplyByStatus(@Param("status") int status,@Param("applicant")int user_id);



    @Update("UPDATE bz_meeting_apply SET app_name = #{app_name} ," +
            "app_description = #{app_description} ," +
            "meeting_time = #{meeting_time} ," +
            "during_time = #{during_time} ," +
            "location = #{location} ," +
            "em_number = #{em_number} ," +
            "em_list = #{em_list} ," +
            "status = #{status} ," +
            "applicant = #{applicant} ," +
            "hardware_applicant = #{hardware_applicant} ," +
            "isIntelligence_allocation = #{isIntelligence_allocation} ," +
            "other = #{other} " +
            "WHERE app_meeting_id = #{app_meeting_id};")
    int updateMeetingApply(@Param("app_meeting_id")int app_meeting_id,
                           @Param("app_name")String app_name,
                           @Param("app_description")String app_description,
                           @Param("meeting_time")Date meeting_time,
                           @Param("during_time")int during_time,
                           @Param("location")int location,
                           @Param("em_number")int em_number,
                           @Param("em_list")String em_list,
                           @Param("status")int status,
                           @Param("applicant")int applicant,
                           @Param("hardware_applicant")String hardware_applicant,
                           @Param("isIntelligence_allocation")int isIntelligence_allocation,
                           @Param("other")String other);

}
